﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::init()
{
    ui->stackedWidget->setCurrentWidget(ui->pagBooks);

//    ui->tvBooks->setEditTriggers(QAbstractItemView::NoEditTriggers);
//    ui->tvInfos->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->tvBooks->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tvInfos->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);

    m_booksModel = new QStandardItemModel(this);
    m_infoModel = new QStandardItemModel(this);

    ui->tvBooks->setModel(m_booksModel);
    ui->tvInfos->setModel(m_infoModel);
}


void MainWindow::on_actLoadBooks_triggered()
{
    QString filePath = QFileDialog::getOpenFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)");
    if(!filePath.isEmpty()){
        ui->stackedWidget->setCurrentWidget(ui->pagBooks);
        m_booksModel->clear();
        m_bookList.clear();

        QFile file(filePath);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        QStringList headers = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
        m_booksModel->setColumnCount(headers.count());
        for(int i = 0; i < headers.count();++i){
            m_booksModel->setHeaderData(i,Qt::Horizontal,headers.at(i));
        }

        for(;!stream.atEnd();){

            m_booksModel->insertRow(m_booksModel->rowCount());
            QStringList datas = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
            for(int i = 0;i < m_booksModel->columnCount();++i){
                m_booksModel->setData(m_booksModel->index(m_booksModel->rowCount() - 1,i),datas.at(i));
            }

            myBook book(datas.at(0),datas.at(1),datas.at(2),datas.at(3),datas.at(4),datas.at(5));
            m_bookList.append(book);
        }
        file.close();
    }
}


void MainWindow::on_actLoadLogInfo_triggered()
{
    QString filePath = QFileDialog::getOpenFileName(0,"选择文件",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)");
    if(!filePath.isEmpty()){
        ui->stackedWidget->setCurrentWidget(ui->pagInfoes);
        m_infoModel->clear();
        m_bookinfoList.clear();
        ui->combBookType->setCurrentIndex(0);

        QFile file(filePath);
        file.open(QIODevice::ReadOnly | QIODevice::Text);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");
        QStringList headers = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
        m_infoModel->setColumnCount(headers.count());
        for(int i = 0; i < headers.count();++i){
            m_infoModel->setHeaderData(i,Qt::Horizontal,headers.at(i));
        }

        for(;!stream.atEnd();){

            m_infoModel->insertRow(m_infoModel->rowCount());
            QStringList datas = stream.readLine().trimmed().split("\t",Qt::SkipEmptyParts);
            for(int i = 0;i < m_infoModel->columnCount();++i){
                m_infoModel->setData(m_infoModel->index(m_infoModel->rowCount() - 1,i),datas.at(i));
            }

            myBookInfo bookinfo(datas.at(0),datas.at(1),datas.at(2),datas.at(3),datas.at(4),datas.at(5));
            m_bookinfoList.append(bookinfo);
        }
        file.close();
    }
}


void MainWindow::on_actBooks_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagBooks);
}


void MainWindow::on_actLog_triggered()
{
    ui->stackedWidget->setCurrentWidget(ui->pagInfoes);
}


void MainWindow::on_actSaveBooks_triggered()
{
    QString filePath = QFileDialog::getSaveFileName(0,"选择保存位置",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)");
    if(!filePath.isEmpty()){
        QFile file(filePath);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");

        for(int i = 0;i < m_booksModel->columnCount();++i){
            stream << m_booksModel->headerData(i,Qt::Horizontal).toString() << "\t";
        }
        stream << "\n";

        for(int i = 0;i < m_booksModel->rowCount();++i){
            for(int j = 0;j < m_booksModel->columnCount();++j){
                stream << m_booksModel->data(m_booksModel->index(i,j)).toString() << "\t";
            }
            stream << "\n";
        }
        file.close();
    }
}


void MainWindow::on_actSaveLogInfo_triggered()
{
    QString filePath = QFileDialog::getSaveFileName(0,"选择保存位置",QDir::homePath(),"文本文件(*.txt);;所有文件(*.*)");
    if(!filePath.isEmpty()){
        QFile file(filePath);
        file.open(QIODevice::WriteOnly | QIODevice::Text);
        QTextStream stream(&file);
        stream.setCodec("UTF-8");

        for(int i = 0;i < m_infoModel->columnCount();++i){
            stream << m_infoModel->headerData(i,Qt::Horizontal).toString() << "\t";
        }
        stream << "\n";

        for(int i = 0;i < m_infoModel->rowCount();++i){
            for(int j = 0;j < m_infoModel->columnCount();++j){
                stream << m_infoModel->data(m_infoModel->index(i,j)).toString() << "\t";
            }
            stream << "\n";
        }
        file.close();
    }
}






